package com.totoro.tymapping.commands;

import com.totoro.tymapping.TyMapping;
import com.totoro.tymapping.constants.CommandConst;
import com.totoro.tymapping.enums.CommandExecutorEnum;
import com.totoro.tymapping.executor.command.MainCommandExecutor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Totoro
 * @version 0.1.0
 * @date 2025/8/10 - 19:45
 * @description
 **/
public class MainCommand implements CommandExecutor, TabExecutor {

    private final TyMapping plugin;

    public MainCommand(TyMapping plugin) {
        this.plugin = plugin;
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if(args.length == 0) {
            CommandExecutorEnum.HELP.getMainCommandExecutor().execute(plugin, sender, args);
            return false;
        }
        CommandExecutorEnum.getMainCommandExecutor(args[0]).execute(plugin, sender, args);
        return false;
    }

    @Override
    public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
        if(!sender.isOp()) {
            return null;
        }
        if(args.length == 1) {
            if(args[0].startsWith("r")) {
                return Collections.singletonList(CommandConst.RELOAD_COMMAND);
            }
            if(args[0].startsWith("s")) {
                if(args[0].startsWith("sta")) {
                    return Collections.singletonList(CommandConst.START_COMMAND);
                }
                if(args[0].startsWith("sto")) {
                    return Collections.singletonList(CommandConst.STOP_COMMAND);
                }
                return Arrays.asList(CommandConst.START_COMMAND, CommandConst.STOP_COMMAND);
            }
            return Arrays.stream(CommandExecutorEnum.values()).map(CommandExecutorEnum::getCommandType).collect(Collectors.toList());
        }
        return null;
    }
}
